#include <QObject>
#include <QString>
#include <QTranslator>
#include <pluginGl.h>
#include <functional>
#include <qsciscintilla.h>
#include "ConversionPlugin.h"
#define NDD_EXPORTDLL

static QWidget* s_pNotepad = nullptr;
static NDD_PROC_DATA s_pProcData = {};
static std::function<QsciScintilla* ()> s_getCurEdit = nullptr;
static ConversionPlugin* g_plugin = nullptr;
static QTranslator s_trans;
#if defined(Q_OS_WIN)
#if defined(NDD_EXPORTDLL)
#define NDD_EXPORT __declspec(dllexport)
#else
#define NDD_EXPORT __declspec(dllimport)
#endif
#else
#define NDD_EXPORT
#endif

extern "C" {
	NDD_EXPORT bool NDD_PROC_IDENTIFY(NDD_PROC_DATA* pProcData) {
		auto lang = QLocale().language();
		auto region = QLocale().country();
		bool loaded = false;
		if (lang == QLocale::Chinese && (region == QLocale::China || region == QLocale::HongKong || region == QLocale::Macau || region == QLocale::Singapore)) {
			loaded = s_trans.load(":/zh_CN_tr.qm");
		} else if (lang == QLocale::Chinese) {
			loaded = s_trans.load(":/zh_TW_tr.qm");
		} else {
			loaded = s_trans.load("");
		}
		if (loaded) {
			qApp->installTranslator(&s_trans);
		}
		pProcData->m_strPlugName = QString(PLUGIN_NAME);
		pProcData->m_version = QString(PLUGIN_VERSION_STR);
		pProcData->m_strComment = QObject::tr("ASCII-HEX Converter");
		pProcData->m_auther = QString(PLUGIN_AUTHOR);
		pProcData->m_strFilePath = QString("");
		pProcData->m_menuType = 1;
		return true;
	}

	NDD_EXPORT int NDD_PROC_MAIN(QWidget* pNotepad, const QString& strFileName, std::function<QsciScintilla* ()>getCurEdit, std::function<bool(int, void*)> pluginCallBack, NDD_PROC_DATA* pProcData) {
		if (pNotepad == nullptr || pProcData == nullptr || getCurEdit == nullptr) {
			return -1;
		}
		s_pNotepad = pNotepad;
		s_pProcData = *pProcData;
		s_getCurEdit = getCurEdit;
		if (g_plugin != nullptr) {
			delete g_plugin;
		}
		g_plugin = new ConversionPlugin(pNotepad, s_getCurEdit, s_pProcData.m_rootMenu, s_pProcData.m_strFilePath);
		return 0;
	};
}
